﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using iTFF.WPF.CP;

namespace iTFF.WPF.Model.Lyrics
{
    public class LyricsModel
    {

        public string Artist { get; set; }
        public string Name { get; set; }
        public string Album { get; set; }
        public string Lyrics { get; set; }

        [XmlIgnore]
        public LyricsWebProvider Provider { get; set; }

        [XmlAttribute]
        public ulong iTunesPersistentID { get; set; }

        #region Serialization properties

        public string ProviderName 
        {
            get 
            { 
                return Provider == null ? null : Provider.Name; 
            }
            set
            {
                Provider = ControlPanel.Instance.LyricsControlPanel.GetWebProvider(value);
            }
        }

        [XmlAttribute]
        public string Hex
        {
            get { return iTunesPersistentID.ToString("X"); }
            set { iTunesPersistentID = ulong.Parse(value, System.Globalization.NumberStyles.HexNumber); }
        }
        
        #endregion

        #region Environment methods

        /// <summary>
        /// Convert \n to \r\n for all Properties
        /// </summary>
        public void ConvertNToRN()
        {
            Artist = ConvertNToRN(Artist);
            Name = ConvertNToRN(Name);
            Album = ConvertNToRN(Album);
            Lyrics = ConvertNToRN(Lyrics);
        }

        /// <summary>
        /// Converts \r to \r\n for the specified string
        /// </summary>
        /// <param name="val"></param>
        /// <returns></returns>
        private string ConvertNToRN(string val)
        {
            return val.Replace("\n", "\r\n");
        }

        #endregion

        public override bool Equals(object obj)
        {
            if (obj is LyricsModel)
            {
                var other = (LyricsModel)obj;
                return Artist == other.Artist && Album == other.Album && Name == other.Name && Lyrics == other.Lyrics
                    && iTunesPersistentID == other.iTunesPersistentID;
            }
            else
            return base.Equals(obj);
        }

        public override int GetHashCode()
        {
            return base.GetHashCode();
        }

    }
}
